package operation;

import Dao.UserDao;
import interfac.UserOperation;
import pojo.User;

import java.util.List;
import java.util.Scanner;

public class DeleteUserOperation implements UserOperation {
    @Override
    public void execute(Scanner scanner) {
        UserDao userDao = new UserDao();
        show_user(); // 显示所有员工，以便用户选择要删除的员工
        System.out.print("请输入要删除的员工ID: ");
        int userIdToDelete = Integer.parseInt(scanner.nextLine());

        User existingUser = userDao.getUserById(userIdToDelete);

        if (existingUser != null) {
            System.out.print("确认要删除此员工吗？(y/n): ");
            String confirmation = scanner.nextLine();
            if ("y".equalsIgnoreCase(confirmation)) {
                userDao.deleteUser(userIdToDelete);
                System.out.println("员工已成功删除");
            } else {
                System.out.println("取消删除操作");
            }
        } else {
            System.out.println("输入的员工ID不存在，请检查后重新输入。");
        }
    }

    private void show_user() {
        UserDao userDao = new UserDao();
        List<User> allUsers = userDao.getUsers();

        if (!allUsers.isEmpty()) {
            System.out.println("----- 员工列表 -----");
            for (User user : allUsers) {
                System.out.printf("ID: %d, 用户名: %s%n", user.getId(), user.getUsername());
            }
            System.out.println("-------------------");
        } else {
            System.out.println("没有找到任何员工记录。");
        }
    }
}